package com.nt.train.controller;
import com.nt.train.service.ProjectService;
import com.nt.train.vo.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 演示多种注解是否可代替 Controller/RestController 处理
 * @author hlf
 * @version 1.0
 * @date 2022/7/28 8:51
 */
@Component
//@Service
//@Repository
@RequestMapping("/index")
public class IndexController {

    @Autowired
    ProjectService projectService;

    //模拟完整controller-service-dao过程
    @GetMapping(value = "/findById/{id}")
    @ResponseBody
    public AjaxResult findById(@PathVariable String id) {
        return AjaxResult.success(projectService.selectById(id));
    }


//    @RequestMapping("/index")
//    public String sayHello(){
//        return "hello spring boot!!" ;
//    }
//
//    @RequestMapping("/index2")
//    @ResponseBody
//    public String sayIndex(){
//        return "hello spring boot!" ;
//    }

    // 重定向至首页
    @GetMapping("/")
    public void root(HttpServletResponse httpServletResponse) throws IOException {
        httpServletResponse.sendRedirect("/index");
    }

    @GetMapping("/index")
    @ResponseBody
    public ModelAndView index(ModelMap modelMap) {
        modelMap.put("userName","刘志强");
        modelMap.put("date",new Date());
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("id",1);
        map1.put("name","张三");
        list.add(map1);
        Map<String,Object> map2 = new HashMap<>();
        map2.put("id",2);
        map2.put("name","李四");
        list.add(map2);
        modelMap.put("list", list);
        return new ModelAndView("/index", modelMap);
    }

}
